package net.csdn.business.discuss.api.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.csdn.business.discuss.api.model.dto.SyncDeleteDTO;
import net.csdn.business.discuss.api.model.dto.TransferCategoryDTO;
import net.csdn.business.discuss.api.model.entity.Discuss;
import net.csdn.business.discuss.api.model.query.DisciussDetailQuery;
import net.csdn.business.discuss.api.model.query.DiscussQuery;
import net.csdn.business.discuss.api.model.vo.DiscussDetailVO;
import net.csdn.business.discuss.api.model.vo.DiscussVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @ClassName DiscussMapper
 * @Description DiscussMapper接口
 * @Author zhangyl
 * @Date 2023/7/18 11:14 
 */
public interface DiscussMapper extends BaseMapper<Discuss>
{

    /**
     * 分页查询讨论列表
     *
     * @param query 字典类型信息
     * @return 字典类型集合信息
     */
    @Results(id="processLable",value={
       @Result(property = "label",column = "label",typeHandler = com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler.class)
    })
    @Select("<script> select t1.object_id,t1.serial_number,t1.title,t1.is_lock,t1.is_pin,t1.is_category_pin,t1.pin_date,t1.pin_category_date,t1.closed_date,t1.is_closed,t1.is_answered,t1.comment_total,t1.reply_total,t1.like_total,t1.label,t1.category_id,t1.created_by,t1.created_date FROM discuss t1  " +
            "where 1=1" +
            "<if test='query.title != null and query.title!=\"\"' >" +
            "AND t1.title like concat('%', #{query.title}, '%') " +
            "</if>" +
            "<if test='query.sourceId != null and query.sourceId!=\"\"' >" +
            "AND t1.source_id = #{query.sourceId} " +
            "</if>" +
            "<if test='query.sourceType != null' >" +
            "AND t1.source_type = #{query.sourceType} " +
            "</if>" +
            "<if test='query.serialNumber != null ' >" +
            "AND t1.serial_number = #{query.serialNumber} " +
            "</if>" +
            "<if test='query.categoryId != null and query.categoryId!=\"\"' >" +
            "AND t1.category_id = #{query.categoryId} " +
            "</if>" +
            "<if test='query.isLock != null ' >" +
            "AND t1.is_lock = #{query.isLock} " +
            "</if>" +
            "<if test='query.isClosed != null and query.isClosed ==\"0\" ' >" +
            "AND t1.is_closed = 0 " +
            "</if>" +
            "<if test='query.isClosed != null and query.isClosed ==\"1\" ' >" +
            "AND t1.is_closed in (1,2,3) " +
            "</if>" +
            "<if test='query.isAnswered != null ' >" +
            "AND t1.is_answered = #{query.isAnswered} " +
            "</if>" +
            "<if test='query.labelId != null and query.labelId!=\"\"'>" +
            "AND json_contains(label,json_array(#{query.labelId})) " +
            "</if>" +
            "order by " +
            "<if test='query.categoryId != null and query.categoryId!=\"\"' >" +
            " t1.pin_category_date desc, " +
            "</if>" +
            "<if test='query.categoryId == null or query.categoryId==\"\"' >" +
            "  t1.pin_date desc, " +
            "</if>" +
            " <choose>" +
            " <when test='query.sort ==0 '>" +
            "   t1.last_modified_date desc  " +
            " </when>" +
            " <when test='query.sort ==1 '>" +
            "   t1.created_date desc  " +
            " </when>" +
            " <when test='query.sort ==2'>" +
            "    t1.created_date asc " +
            " </when>" +
            " <when test='query.sort ==3'>" +
            "    t1.comment_total desc " +
            " </when>" +
            " <when test='query.sort ==4'>" +
            "   t1.comment_total asc " +
            " </when>" +
            " </choose>" +
            "</script>")
    public List<DiscussVO> findPageList(Page page, @Param("query") DiscussQuery query);


    @Select("select count(*) from discuss where source_id= #{sourceId} and source_type= #{sourceType}")
    public Integer getTotal(@Param("sourceId")String sourceId, @Param("sourceType")Integer sourceType);

    /**
     * 查询讨论
     * 
     * @param id 讨论主键
     * @return 讨论
     */
    @Select("select * from discuss where object_id=#{id}")
    public DiscussVO selectDiscussById(@Param("id")String id);


    /**
     * 根据sourceId,sourceType、serialNumber查询讨论
     *
     * @param query 查询参数
     * @return 讨论
     */
    @Results(id="processLable1",value={
            @Result(property = "label",column = "label",typeHandler = com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler.class)
    })
    @Select("select * from discuss where source_id=#{query.sourceId} and source_type=#{query.sourceType} and serial_number= #{query.serialNumber}")
    public DiscussVO selectDiscussDetail(@Param("query") DisciussDetailQuery query);

    /**
     * 查询讨论列表
     * 
     * @param query 讨论
     * @return 讨论集合
     */
    public List<DiscussVO> selectDiscussList(@Param("query") DiscussQuery query);

    /**
     * 新增讨论
     * 
     * @param discuss 讨论
     * @return 结果
     */
    public Integer insertDiscuss(Discuss discuss);

    /**
     * 修改讨论
     *
     * @param discuss 讨论
     * @return 结果
     */
    public Integer updateDiscuss(Discuss discuss);

    /**
     * 删除讨论
     * 
     * @param params 请求参数
     * @return 结果
     */
    @Delete("delete from discuss where source_id = #{params.sourceId} AND source_type = #{params.sourceType}")
    public Integer syncDelete(@Param("params")SyncDeleteDTO params);

    @Update("update discuss set comment_total=comment_total+#{sum} where object_id=#{id}")
    public Integer updateCommentTotal(@Param("id")String id, @Param("sum")Integer sum);


    @Update("update discuss set reply_total=reply_total+#{sum} where object_id=#{id}")
    public Integer updateReplyTotal(@Param("id")String id, @Param("sum")Integer sum);

    @Update("update discuss set like_total=like_total+#{sum} where object_id=#{id}")
    public Integer updateLikeTotal(@Param("id")String id, @Param("sum")Integer sum);

    @Update("update discuss set category_id=#{params.targetId} where category_id=#{params.id}")
    public Integer  transferCategory(@Param("params")TransferCategoryDTO params);

    @Select("select source_id from discuss where object_id=#{discussId}")
    public String selectSourceId(@Param("discussId") String discussId);




    /**
     * 分页查询讨论列表
     *
     * @param query 字典类型信息
     * @return 字典类型集合信息
     */
    @Results(id="processLable2",value={
            @Result(property = "label",column = "label",typeHandler = com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler.class)
    })
    @Select("<script> select t1.object_id,t1.serial_number,t1.title,t1.is_lock,t1.is_pin,t1.is_category_pin,t1.pin_date,t1.pin_category_date,t1.closed_date,t1.is_closed,t1.is_answered,t1.comment_total,t1.reply_total,t1.like_total,t1.label,t1.category_id,t1.created_by,t1.created_date FROM discuss t1  " +
            "where created_by= #{query.currUserId} " +
            "<if test='query.title != null and query.title!=\"\"' >" +
            "AND t1.title like concat('%', #{query.title}, '%') " +
            "</if>" +
            "<if test='query.sourceId != null and query.sourceId!=\"\"' >" +
            "AND t1.source_id = #{query.sourceId} " +
            "</if>" +
            "<if test='query.sourceType != null' >" +
            "AND t1.source_type = #{query.sourceType} " +
            "</if>" +
            "<if test='query.serialNumber != null ' >" +
            "AND t1.serial_number = #{query.serialNumber} " +
            "</if>" +
            "<if test='query.categoryId != null and query.categoryId!=\"\"' >" +
            "AND t1.category_id = #{query.categoryId} " +
            "</if>" +
            "<if test='query.isLock != null ' >" +
            "AND t1.is_lock = #{query.isLock} " +
            "</if>" +
            "<if test='query.isClosed != null and query.isClosed ==\"0\" ' >" +
            "AND t1.is_closed = 0 " +
            "</if>" +
            "<if test='query.isClosed != null and query.isClosed ==\"1\" ' >" +
            "AND t1.is_closed in (1,2,3) " +
            "</if>" +
            "<if test='query.isAnswered != null ' >" +
            "AND t1.is_answered = #{query.isAnswered} " +
            "</if>" +
            "<if test='query.labelId != null and query.labelId!=\"\"'>" +
            "AND json_contains(label,json_array(#{query.labelId})) " +
            "</if>" +
            "order by " +
            "<if test='query.categoryId != null and query.categoryId!=\"\"' >" +
            " t1.pin_category_date desc, " +
            "</if>" +
            "<if test='query.categoryId == null or query.categoryId==\"\"' >" +
            "  t1.pin_date desc, " +
            "</if>" +
            " <choose>" +
            " <when test='query.sort ==1 '>" +
            "   t1.created_date desc  " +
            " </when>" +
            " <when test='query.sort ==2'>" +
            "    t1.created_date asc " +
            " </when>" +
            " <when test='query.sort ==3'>" +
            "    t1.comment_total desc " +
            " </when>" +
            " <when test='query.sort ==4'>" +
            "   t1.comment_total asc " +
            " </when>" +
            " </choose>" +
            "</script>")
    public List<DiscussVO> myDiscussPageList(Page page, @Param("query") DiscussQuery query);


    /**
     * 分页查询讨论列表
     *
     * @param query 字典类型信息
     * @return 字典类型集合信息
     */
    @Results(id="processLable3",value={
            @Result(property = "label",column = "label",typeHandler = com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler.class)
    })
    @Select("<script> select t1.object_id,t1.serial_number,t1.title,t1.is_lock,t1.is_pin,t1.is_category_pin,t1.pin_date,t1.pin_category_date,t1.closed_date,t1.is_closed,t1.is_answered,t1.comment_total,t1.reply_total,t1.like_total,t1.label,t1.category_id,t1.created_by,t1.created_date " +
            "FROM discuss t1 inner JOIN ( SELECT  discuss_id,max(created_date) as created_date FROM discuss_activity where uid ='64d04f098d009f3d8263ab60' GROUP BY discuss_id ) t ON t1.object_id = t.discuss_id " +
            "where 1=1 " +
            "<if test='query.title != null and query.title!=\"\"' >" +
            "AND t1.title like concat('%', #{query.title}, '%') " +
            "</if>" +
            "<if test='query.sourceId != null and query.sourceId!=\"\"' >" +
            "AND t1.source_id = #{query.sourceId} " +
            "</if>" +
            "<if test='query.sourceType != null' >" +
            "AND t1.source_type = #{query.sourceType} " +
            "</if>" +
            "<if test='query.serialNumber != null ' >" +
            "AND t1.serial_number = #{query.serialNumber} " +
            "</if>" +
            "<if test='query.categoryId != null and query.categoryId!=\"\"' >" +
            "AND t1.category_id = #{query.categoryId} " +
            "</if>" +
            "<if test='query.isLock != null ' >" +
            "AND t1.is_lock = #{query.isLock} " +
            "</if>" +
            "<if test='query.isClosed != null and query.isClosed ==\"0\" ' >" +
            "AND t1.is_closed = 0 " +
            "</if>" +
            "<if test='query.isClosed != null and query.isClosed ==\"1\" ' >" +
            "AND t1.is_closed in (1,2,3) " +
            "</if>" +
            "<if test='query.isAnswered != null ' >" +
            "AND t1.is_answered = #{query.isAnswered} " +
            "</if>" +
            "<if test='query.labelId != null and query.labelId!=\"\"'>" +
            "AND json_contains(label,json_array(#{query.labelId})) " +
            "</if>" +
            "ORDER BY t.created_date DESC " +
            "</script>")
    public List<DiscussVO> joinDiscussPageList(Page page, @Param("query") DiscussQuery query);

}
